﻿using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;

namespace Neocortex.WPF.Hermetica
{
	public class PdfManager
	{
		public PdfManager(string path)
		{
			FullName = path;
		}

		public string FullName { get; set; }

		public Image Thumbnail { get; private set; }

		public Image GetImage()
		{
			var ram = new RandomAccessFileOrArray(FullName);
			var reader = new PdfReader(ram, null);

			for (int i = 0; i <= reader.XrefSize - 1; i++)
			{
				try
				{
					var pdfObject = reader.GetPdfObjectRelease(i);
					if (pdfObject != null && pdfObject.IsStream())
					{
						var stream = pdfObject as PdfStream;
						var subtype = stream.Get(PdfName.SUBTYPE);
						if (subtype != null && subtype.ToString() == PdfName.IMAGE.ToString())
						{
							var a = stream.Get(PdfName.SUBTYPE);

							var bytes = PdfReader.GetStreamBytesRaw(stream as PRStream);

							using (var ms = new MemoryStream(bytes))
							{
								var image = Image.FromStream(ms);

								using (var ms2 = new MemoryStream())
								{
									image.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
									ms2.Position = 0;
									image = Image.FromStream(ms2);
								}

								Graphics g = Graphics.FromImage(image);
								g.DrawString(".", new Font(FontFamily.GenericSansSerif, 12), Brushes.Red, new PointF(10, 10));
								Thumbnail = image;
								break;
							}
						}
					}
				}
				catch (Exception ex)
				{

				}
			}

			reader.Close();
			ram.Close();
			return Thumbnail;
		}
		public List<Image> GetImages()
		{
			var result = new List<Image>();

			var ram = new RandomAccessFileOrArray(FullName);
			var reader = new PdfReader(ram, null);

			for (int i = 0; i <= reader.XrefSize - 1; i++)
			{
				var pdfObject = reader.GetPdfObjectRelease(i);
				if (pdfObject != null && pdfObject.IsStream())
				{
					var stream = pdfObject as PdfStream;
					var subtype = stream.Get(PdfName.SUBTYPE);
					if (subtype != null && subtype.ToString() == PdfName.IMAGE.ToString())
					{
						var a = stream.Get(PdfName.SUBTYPE);

						var bytes = PdfReader.GetStreamBytesRaw(stream as PRStream);

						try
						{
							using (var ms = new MemoryStream(bytes))
							{
								var image = Image.FromStream(ms);

								using (var ms2 = new MemoryStream())
								{
									image.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
									ms2.Position = 0;
									image = Image.FromStream(ms2);
								}

								Graphics g = Graphics.FromImage(image);
								g.DrawString(".", new Font(FontFamily.GenericSansSerif, 12), Brushes.Red, new PointF(10, 10));
								result.Add(image);
							}
						}
						catch (Exception ex)
						{
							/// ...ignore
						}
					}
				}
			}

			reader.Close();
			ram.Close();

			return result;
		}
	}
}
